package cao.oschina.bean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * 提醒
 * <p/>
 * Created by Haidy on 15/3/13.
 */
public class Notice {

    public String atmeCount;
    public String msgCount;
    public String reviewCount;
    public String newFansCount;

    public static Notice parser(String xml) {
        Notice notice = new Notice();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
            Element root = document.getDocumentElement();
            Node noticeNode = root.getElementsByTagName("notice").item(0);
            NodeList list = noticeNode.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Node node = list.item(i);
                    switch (node.getNodeName()) {
                        case "atmeCount":
                            notice.atmeCount = node.getFirstChild().getNodeValue();
                            break;
                        case "msgCount":
                            notice.msgCount = node.getFirstChild().getNodeValue();
                            break;
                        case "reviewCount":
                            notice.reviewCount = node.getFirstChild().getNodeValue();
                            break;
                        case "newFansCount":
                            notice.newFansCount = node.getFirstChild().getNodeValue();
                            break;
                    }
                }
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return notice;
    }
}
